using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        ManageBuildData.TfsDataClient tfsClient;

        private void button1_Click(object sender, EventArgs e)
        {   
            toolStripStatusLabel2.Text = "Getting build types from TFS";
            this.Refresh();
            Cursor.Current = Cursors.WaitCursor;
          
            tfsClient = new ManageBuildData.TfsDataClient();
            tfsClient.init(textBox1.Text, textBox2.Text);
            string[] buildTypes = tfsClient.GetBuildTypes();
            comboBox1.SelectedIndexChanged -= new EventHandler(this.comboBox1_SelectedIndexChanged);
            comboBox1.DataSource = buildTypes;
            comboBox1.SelectedIndexChanged +=new EventHandler(comboBox1_SelectedIndexChanged);
            tabControl1.Enabled = true;
            Cursor.Current = Cursors.Default;
            toolStripStatusLabel2.Text = "";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Getting builds for build type " + comboBox1.Text;
            this.Refresh();
            Cursor.Current = Cursors.WaitCursor;
            string[] builds = tfsClient.GetBuilds(comboBox1.Text);
            listBox1.DataSource = builds;
            tabControl1.TabStop = true;
            TabPage tab = this.tabControl1.TabPages[1];
            this.tabControl1.SelectedTab = tab;
            Cursor.Current = Cursors.Default;
            toolStripStatusLabel1.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Getting changesets which were added after the build " + listBox1.SelectedItem.ToString() + " was executed";
            this.Refresh();
            Cursor.Current = Cursors.WaitCursor;         
            List<int> NonAssociatedChangeSets = null;
            List<string> NonAssociatedChangeSetComments = null;
            try
            {
                NonAssociatedChangeSets = tfsClient.GetChangeSetsAfterBuild(listBox1.SelectedItem.ToString()); //Build Number is the parameter
                NonAssociatedChangeSetComments = tfsClient.GetChangeSetComments(NonAssociatedChangeSets);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                listView2.Clear();
                return;
            }
            
            splitContainer1.Enabled = true;
            listView2.ItemChecked -= new ItemCheckedEventHandler(listView2_ItemChecked);
            listView2.Clear();
            listView2.Columns.Add("ChangeSet Id", 70);
            listView2.Columns.Add("ChangeSet Comment", 700);

            for (int i = 0; i < NonAssociatedChangeSets.Count; i++)
            {
                ListViewItem lvi = new ListViewItem(NonAssociatedChangeSets[i].ToString());
                lvi.SubItems.Add(NonAssociatedChangeSetComments[i]);
                listView2.Items.Add(lvi);

            }   
            listView2.ItemChecked +=new ItemCheckedEventHandler(listView2_ItemChecked);
            toolStripStatusLabel1.Text = "";
        }
        //List<int> SelectedWorkItems;
        private void CreateLabel()
        {
            List<int> AssociatedChangeSets = tfsClient.GetChangesetNumbers(listBox1.SelectedItem.ToString());
           
            List<int> SelectedChangeSets = new List<int>();
             

            foreach (ListViewItem checkedItem in listView2.CheckedItems)
            {
                
                //foreach(int changeSet in SelectedChangeSets)
                //{
                    SelectedChangeSets.Add(Int32.Parse(checkedItem.SubItems[0].Text));
                //}
                
            }
            foreach (int AssociatedChangeSet in AssociatedChangeSets)
            {
                SelectedChangeSets.Add(AssociatedChangeSet);
            }

            tfsClient.SetLabel(SelectedChangeSets, textBox3.Text, listBox1.SelectedItem.ToString());
            }
        
        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox3.Text != "")
            {
                Cursor.Current = Cursors.WaitCursor;
                toolStripStatusLabel1.Text = "Creating label for the build";
                this.Refresh();
                this.CreateLabel();
                toolStripStatusLabel1.Text = "Executing build of labeled items";
                this.Refresh();
                bool buildSuccess = tfsClient.ModifyTfsBuildProj("TempForBuild", listBox1.SelectedItem.ToString());
                if (buildSuccess)
                {
                    toolStripStatusLabel1.Text = "Build started successfully, wait for a few minutes to check the build quality";
                    tfsClient.BuildCompleted += new EventHandler(tfsClient_BuildCompleted);
                }
                else
                    toolStripStatusLabel1.Text = "This build could not be started. Check build log for further information";
                Cursor.Current = Cursors.Default;
            }
            
        }

        void tfsClient_BuildCompleted(object sender, EventArgs e)
        {
            tfsClient.DeleLabel();
            MessageBox.Show("Build Execution Over");
        }

        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            
        }

        //This is a special method for customization requirement
        //private void GetWorkItems()
        //{
        //    List<string> changeSets = tfsClient.GetChangeSetsForWorkitems(int.Parse(listView2.CheckedItems[0].SubItems[0].Text));
        //    DataTable AllAssociatedWorkItems = new DataTable();
        //    AllAssociatedWorkItems.Columns.Add("Id", typeof(int));
        //    AllAssociatedWorkItems.Columns.Add("Title", typeof(string));
        //    AllAssociatedWorkItems.PrimaryKey = new DataColumn[] { AllAssociatedWorkItems.Columns["Id"] };
        //    foreach (string changeSet in changeSets)
        //    {
        //        List<int> HistoryOfFiles = new List<int>();
        //        HistoryOfFiles = tfsClient.GetHistoryOfFilesInChangeSet(int.Parse(changeSet));
        //        foreach (int HistoricalChangeSet in HistoryOfFiles)
        //        {
        //            DataTable HistoricalWorkItems = tfsClient.GetWorkItemsForChangeSet(HistoricalChangeSet);
        //            for (int i = 0; i < HistoricalWorkItems.Rows.Count; i++)
        //            {
        //                try
        //                {
        //                    AllAssociatedWorkItems.ImportRow(HistoricalWorkItems.Rows[i]);
        //                }
        //                catch
        //                {
        //                }
        //            }

        //        }

        //    }
        //    listView1.Clear();
        //    listView1.Columns.Add("WorkItem Id", 70);
        //    listView1.Columns.Add("WorkItem Title", 240);
            
        //    for (int i = 0; i < AllAssociatedWorkItems.Rows.Count; i++)
        //    {
        //        DataRow dRow = AllAssociatedWorkItems.Rows[i];
        //        ListViewItem lvi = new ListViewItem(dRow["Id"].ToString());
        //        lvi.SubItems.Add(dRow["Title"].ToString());
        //        bool dupExist = false;
        //        foreach(ListViewItem lv in listView1.Items)
        //        {
        //            if (lv.Text == lvi.Text)
        //            {
        //                dupExist = true;
        //                break;
        //            }
        //        }
        //        if (dupExist == false)
        //            listView1.Items.Add(lvi);
        //        foreach (ListViewItem lvi1 in listView2.Items)
        //        {
        //            if (lvi.Text == lvi1.Text && lvi1.Checked == true)
        //                listView1.Items[i].Checked = true;
        //        }

        //    }
        //}

        private void Form2_Load(object sender, EventArgs e)
        {
            
        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void comboBox1_MouseClick(object sender, MouseEventArgs e)
        {
            
        }

        private void comboBox1_TextUpdate(object sender, EventArgs e)
        {
           
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void listView2_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (e.Item.Checked == true)
            {
                Cursor.Current = Cursors.WaitCursor;
                e.Item.Checked = true;
                listView2.Refresh();
                //GetWorkItems();
                Cursor.Current = Cursors.Default;
            }
            else
            {
               listView2.Refresh();
                
            }
            
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}